using Castle.Core.Interceptor;
using uNhAddIns.NHibernateTypeResolver;
using NHibernate.Proxy;

namespace uNhAddIns.ComponentBehaviors.Castle
{
	[Behavior(1, typeof (IWellKnownProxy))]
	public class GetEntityNameBehavior : IInterceptor
	{
		#region IInterceptor Members

		public void Intercept(IInvocation invocation)
		{
			if (invocation.Method.DeclaringType.Equals(typeof (IWellKnownProxy)))
			{
				if (invocation.Method.Name == "get_EntityName")
				{
					invocation.ReturnValue = invocation.Proxy.GetType().BaseType.FullName;
				}
				else if (invocation.Method.Name == "get_EntityType")
				{
					invocation.ReturnValue = invocation.Proxy.GetType().BaseType;
				}
			}
            //else if (invocation.Method.DeclaringType.Equals(typeof(INHibernateProxy)))
            //{
               
            //    if (invocation.Method.Name == "get_HibernateLazyInitializer")
            //    {
            //        invocation.Proceed();
            //        //var propInfo = invocation.Proxy.GetType().GetProperty("HibernateLazyInitializer");
            //        //invocation.ReturnValue = propInfo.GetValue(invocation.Proxy, null);
            //    }

               
            //}
            else
            {
                invocation.Proceed();
            }
		}

		#endregion
	}
}